package org.jenijo.querydroid;

import org.jenijo.querydroid.R.layout;
import org.jenijo.querydroid.model.Question;
import org.jenijo.querydroid.model.Reponse;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class QuestionActivity extends Activity implements View.OnClickListener , OnCheckedChangeListener{
	private TextView tvQuestNumber, tvQuestion;
	private RadioGroup rGroup;
	private Button butNext;
	private Question question;
	private int maxQuestions;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		setContentView(R.layout.question);
		tvQuestNumber = (TextView) findViewById(R.id.tvQuestNumber);
		tvQuestion = (TextView) findViewById(R.id.tvQuestion);
		rGroup = (RadioGroup) findViewById(R.id.rGroup);
		butNext = (Button) findViewById(R.id.butNext);
		
		butNext.setOnClickListener(this);
		rGroup.setOnCheckedChangeListener(this);
		
		Intent data = getIntent();
		Bundle bundle = data.getExtras();
		question = (Question) bundle.get("question");
		maxQuestions = bundle.getInt("maxQuestions");
		
		tvQuestNumber.setText(getString(R.string.questionTextView)+" "+(question.getNum_quest())+" "+getString(R.string.onTextView)+" "+maxQuestions);
		
		tvQuestion.setText(question.getEnonce());
		
		for(Reponse r : question.getReponses()){
			RadioButton rB = (RadioButton)LayoutInflater.from(this).inflate(R.layout.reponse_button,null);
			rB.setText(r.getNum_rep()+" - "+r.getTexte());
			rB.setId(r.getNum_rep());
			rGroup.addView(rB);
		}
	}
	
	public void onClick(View v) {
		int num_rep = rGroup.getCheckedRadioButtonId();
		if(num_rep > 0){
			Intent i = new Intent();
			i.putExtra("num_quest", question.getNum_quest());
			i.putExtra("num_rep", num_rep);
			setResult(RESULT_OK, i);
			finish();
		}else{
			Toast.makeText(this, R.string.noAnswerSelected,Toast.LENGTH_SHORT).show();
		}
	}
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		
	}
}
